package com.example.waibao.Component;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.example.waibao.entity.LoginUser;
import com.example.waibao.entity.User;
import com.example.waibao.utils.Jwtutil;
import com.example.waibao.utils.RedisCache;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

@Component
public class UserHelper {

    @Autowired
    private RedisCache redisUtil;

    @Autowired
    private RedisCache redisCache;
    /**
     * 获取当前用户的信息
     * @return userVO
     */
    public User getCurrentUser() {
        ServletRequestAttributes sra = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if (sra == null) {
            return null;
        }
        HttpServletRequest request = sra.getRequest();
        return getCurrentUser(request);
    }

    /**
     * 通过请求获取当前用户信息
     * @param request 请求
     * @return userVO
     */
    public User getCurrentUser(HttpServletRequest request) {
        String token = request.getHeader("token");
        if (!StringUtils.hasText(token)) {
            return null;
        }
        return getCurrentUserByToken(token);
    }

    /**
     * 通过token获取当前用户信息
     * @param token token
     * @return userVO
     */
    public User getCurrentUserByToken(String token) {

        //解析token
        String userid;
        try {
            Claims claims = Jwtutil.parseJWT(token);
            userid = claims.getSubject();
        } catch (Exception e) {
            throw new RuntimeException("token非法");
        }

        //从redis中获取用户信息
        String redisKey = "login:" + userid;
        //反序列化
        LoginUser loginUser = redisCache.getCacheObject(redisKey);
        if (Objects.isNull(loginUser)) {
            throw new RuntimeException("token失效,用户未登录");
        }

//        if (Objects.isNull(context)) {
//            return null;
//        }
//        try {
//            return JSONObject.parseObject(context, User.class);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
        return loginUser.getUser();
    }

    /**
     * 获取当前用户的id
     * @return 用户id
     */
    public Long getCurrentUserId() {
        User userVO = getCurrentUser();
        if (ObjectUtil.isNull(userVO)) {
            return null;
        }
        return userVO.getId();
    }

    /**
     * 获取当前请求
     * @return request
     */
    public HttpServletRequest getHttpServletRequest() {
        ServletRequestAttributes sra = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if (sra == null) {
            return null;
        }
        return sra.getRequest();
    }

}

